package tq.com.tqteacher.activity.newPublish;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.adapt.TQJPLeaveMsgAdapter;
import tq.com.tqteacher.adapt.newPublish.TQLeaveMsgAdapter;
import tq.com.tqteacher.discuss.activity.TQDiscussMngActivity;
import tq.com.tqteacher.discuss.activity.TQDiscussMsgActivity;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQCommentOverInfoModel;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQTWDYPublishModel;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQQNoManagerView;

public class TQWKLeaveMsg extends BaseTitleActivity {
    private  TQWKLeaveMsgFragment mLeaveMsgFragment = new TQWKLeaveMsgFragment();

    @Override
    protected BaseFragment fragment() {
        return mLeaveMsgFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);

        if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_QTFX){
            setHeaderTitle("请ta分享");
        }else if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_TWDY){
            setHeaderTitle("提问&群答疑");
        }else {
            setHeaderTitle("微课");
        }
    }

    static public class TQWKLeaveMsgFragment extends BaseRecyclerFragment{
        private TQLeaveMsgAdapter mLeaveMsgAdapter;
        private int page = 1;

        private TQEventBus mEventBus;
        private TQAtachModel mCurrentAttach;
        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);

            TQLeaveMsgAdapter.Type lType = TQLeaveMsgAdapter.Type.wk;
            if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_QTFX){
                lType = TQLeaveMsgAdapter.Type.qtfx;
            }else if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_TWDY){
                lType = TQLeaveMsgAdapter.Type.twdy;
            }

            if(mLeaveMsgAdapter == null){
                mLeaveMsgAdapter = new TQLeaveMsgAdapter(getContext(), lType);
                mLeaveMsgAdapter.setIClickListener(mClickListener);

                if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_TWDY){
                    TQTWDYPublishModel lModel = (TQTWDYPublishModel) TQPublishType.getSharePublishItem();

                    TQQNoManagerView.TQRegionData lRegionData = lModel.firstRegion();
                    if(lRegionData != null){
                        mLeaveMsgAdapter.setStartNo(Integer.valueOf(lRegionData.start));
                    }
                }
            }
            pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            pRecyclerView.setAdapter(mLeaveMsgAdapter);

            if(mEventBus == null){
                mEventBus = new TQEventBus(mEventLisnter);
                mEventBus.register();
            }
        }

        @Override
        protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
            pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    page++;
                    getData();
                }
            });
            pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    page = 1;
                    getData();
                }
            });
            pRefreshLayout.autoRefresh();
        }

        @Override
        public void onStart() {
            super.onStart();
            mCurrentAttach = null;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if(mEventBus != null){
                mEventBus.unregister();
            }
        }

        private void getData(){
            TQLoadingHelper.build(getContext()).setCancelable(false).show();
            TQNetInterface.getMeterialComment(TQPublishType.getSharePublishItem().getSeq_id(), page, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(mRefreshLayout.getState() == RefreshState.Loading){
                        mRefreshLayout.finishLoadMore();
                    }else{
                        mRefreshLayout.finishRefresh();
                    }
                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    ArrayList<TQAtachModel> lTQAtachModels = new ArrayList<>();
                    try {
                        if(pNetResult.getData() == null) return;

                        JSONArray all = pNetResult.getData().optJSONArray("list");
                        if(all == null) return;

                        for (int i = 0; i < all.length(); i++){
                            JSONObject lObject = all.optJSONObject(i);
                            if(lObject == null)continue;

                            JSONObject att = lObject.optJSONObject("attachment");
                            if(att == null)continue;

                            TQAtachModel lAtachModel = new TQAtachModel();
                            lAtachModel.fromJson(att);

                            JSONObject comm = lObject.optJSONObject("comment");
                            if(comm != null){
                                lAtachModel.refreshComment(comm);
                            }
                            lTQAtachModels.add(lAtachModel);
                        }

                        if(page == 1){
                            mLeaveMsgAdapter.clear();
                        }
                        mLeaveMsgAdapter.append(lTQAtachModels);

                        switchEmpty(mLeaveMsgAdapter.getItemCount() == 0);
                    }finally {
                        TQLoadingHelper.dimiss();
                    }
                }
            });

        }

        @Override
        protected void reload() {
            page = 1;
            getData();
        }

        private TQLeaveMsgAdapter.IClickListener mClickListener = new TQLeaveMsgAdapter.IClickListener() {
            @Override
            public void openComment(TQAtachModel pModel, String title) {
                if(pModel == null) return;

                int attId = pModel.getId();
                if(attId == 0) return;

                mCurrentAttach = pModel;
                int mId = TQPublishType.getSharePublishItem().getSeq_id();
                if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.home){
                    TQDiscussMsgActivity.show(mId, attId, title,TQWKLeaveMsgFragment.this.getActivity());
                }else{
                    if(pModel.getComment() == null) return;
                    TQDiscussMngActivity.show(mId, attId, TQWKLeaveMsgFragment.this.getActivity());
                }

            }
        };

        private TQEventBus.IEventLisnter mEventLisnter = new TQEventBus.IEventLisnter() {
            @Override
            public void onEvnet(TQEventBus.TQEventMessage msg) {
                if(msg.getOperator() == TQEventBus.TQEventMessage.Opertor.add
                        && msg.getInfo() instanceof TQCommentOverInfoModel.TQCommentInfoModel){

                    if(mCurrentAttach != null){
                        mCurrentAttach.setCommentInfoModel((TQCommentOverInfoModel.TQCommentInfoModel) msg.getInfo());
                        mLeaveMsgAdapter.update(mCurrentAttach);
                    }
                }else if(msg.getOperator() == TQEventBus.TQEventMessage.Opertor.delete
                        && msg.getInfo() instanceof TQCommentOverInfoModel.TQCommentInfoModel){
                    if(mCurrentAttach.getComment().equals(msg.getInfo()) && mCurrentAttach != null){
                        mCurrentAttach.setCommentInfoModel(null);
                        mLeaveMsgAdapter.update(mCurrentAttach);
                    }
                }
            }
        };
    }
}
